package common

import (
	"fmt"
	"github.com/go-redis/redis"
	"time"
)

type CacheConn struct{
	RedisIp   	string
	RedisPort  	string
	Requirepass string
}

type RdsCcWorker struct {
    r *redis.Client
}

func Redis1nd_CacheHandle(conn CacheConn) *RdsCcWorker {
	var ccw = new(RdsCcWorker)

	ccw.r = redis.NewClient(&redis.Options{
		Addr:     conn.RedisIp + ":" + conn.RedisPort,
		Password: conn.Requirepass,
	})
	_, err := ccw.r.Ping().Result()
	if err != nil {
		panic("ERROR/> Redis Server connect failed.")
		return nil
	}

    return ccw
}

func (ccw *RdsCcWorker) Redis1nd_IsKeyExists(k string) bool {
	a, err := ccw.r.Exists(k).Result()
	if err != nil {
		fmt.Printf("ERROR/> key[%s] doesn't exist.\n", k)
		return false
	}
	if a == 1 {
		fmt.Printf("key[%s] exists.\n", k)
		return true
	}
	return false
}

func (ccw *RdsCcWorker) Redis1nd_KeyValStore(k string, v string, expire int64) bool {
	err := ccw.r.Set(k, v, time.Duration(expire)*time.Second).Err()
	if err != nil {
		fmt.Printf("ERROR/> Store k[%s]:v[%s] failed.\n", k,v)
		return false
	}else{
		fmt.Printf("Store k[%s]:v[%s] successfully.\n", k,v)
		return true
	}
}

func (ccw *RdsCcWorker) Redis1nd_KeyValEject(k string) string {
	value, err := ccw.r.Get(k).Result()
	if err != nil {
		fmt.Printf("ERROR/> Get k[%s] value failed.\n", k)
		return ""
	}else{
		fmt.Printf("Eject k[%s]:v[%s] successfully.\n", k, value)
		return value
	}
}

func (ccw *RdsCcWorker) Redis1nd_KeyExpire(k string, expire int64) bool {
	err := ccw.r.Expire(k, time.Duration(expire)*time.Second).Err()
	if err != nil {
		fmt.Printf("ERROR/> Set expire time failed.\n")
		return false
	}else{
		fmt.Printf("Set k[%s] expired after [%d] seconds.\n", k, expire)
		return true
	}
}

func (ccw *RdsCcWorker) Redis1nd_KeyDelete(k string) bool {
	statusDel, err := ccw.r.Del(k).Result()
	err = err
	if 1 == statusDel {
		fmt.Printf("Delete k[%s] successfully.\n", k)
		return true
	}else{
		fmt.Printf("Delete k[%s] failed.\n", k)
		return false
	}
}

func (ccw *RdsCcWorker) Redis1nd_HashValStore(h string, f string, v string) bool {
	status, err := ccw.r.HSet(h, f, v).Result()
	if err != nil {
		fmt.Printf("ERROR/> Set Hash data struct h[%s]:f[%s]:v[%s] failed.\n", h,f,v)
		return false
	}
	if true == status {
		fmt.Printf("Set Hash value h[%s]:f[%s]:v[%s] successfully.\n", h,f,v)
		return true
	} else {
		fmt.Printf("Update h[%s]:f[%s]:v[%s] successfully.\n", h,f,v)
		return true
	}
}

func (ccw *RdsCcWorker) Redis1nd_HashValEject(h string, f string) string {
	value, err := ccw.r.HGet(h, f).Result()
	if err != nil {
		fmt.Printf("ERROR/> Get h[%s]:f[%s] value failed.\n", h,f)
		return ""
	}else{
		fmt.Printf("Get h[%s]:f[%s]:v[%s] successfully.\n", h,f,value)
		return value
	}
}

func (ccw *RdsCcWorker) Redis1nd_IsHashFieldExists(h string, f string) bool {
	status, _ := ccw.r.HExists(h, f).Result()
	if true == status {
		fmt.Printf("h[%s]:f[%s] field exists.\n", h, f)
		return true
	} else {
		fmt.Printf("h[%s]:f[%s] field doesn't exist.\n", h, f)
		return false
	}
}

func (ccw *RdsCcWorker) Redis1nd_HashValDelete(h string, f string) bool {
	statusDel, err := ccw.r.HDel(h, f).Result()
	if err != nil {
		fmt.Printf("ERROR/> Delete h[%s]:f[%s] field failed.\n",h,f)
		return false
	}
	if 1 == statusDel {
		fmt.Printf("Delete h[%s]:f[%s] field complish.\n",h,f)
		return true
	}
	return false
}

/*
func main() {
	//redis连接参数
	conn := CacheConn{
		RedisIp: "172.24.0.237",
		RedisPort: "16379",
		Requirepass: "4aKadedr",
	}

	//redis 连接实例
	ccw := CacheHandle(conn)
	//完整写入一个Hash数据结构
	ccw.HashValStore("demo2", "UserId", "5")
	ccw.HashValStore("demo2", "Username", "ljquan")
	ccw.HashValStore("demo2", "Password", "Passw0rd")
	ccw.HashValStore("demo2", "Email", "ljquan@supor.com")
	ccw.HashValStore("demo2", "Hiredate", "2018-04-03")
	//完整写入一个Hash数据结构
	ccw.HashValStore("demo1", "UserId", "3")
	ccw.HashValStore("demo1", "Username", "mary")
	ccw.HashValStore("demo1", "Password", "12345678")
	ccw.HashValStore("demo1", "Email", "mary@qq.com")
	ccw.HashValStore("demo1", "Hiredate", "2018-06-06")
	//完整写入一个Hash数据结构
	ccw.HashValStore("demo8", "UserId", "2")
	ccw.HashValStore("demo8", "Username", "lsen")
	ccw.HashValStore("demo8", "Password", "JNkko&4f")
	ccw.HashValStore("demo8", "Email", "lsen@nasa.com")
	ccw.HashValStore("demo8", "Hiredate", "2040-08-08")

	//设置数据结构过期时间: 30秒后自动消失
	ccw.KeyExpire("demo1", 30)
	
	//更新Hash value
	ccw.HashValStore("demo2", "Hiredate", "2018-04-06")
	//删除Hash field
	ccw.HashValDelete("demo2", "Hiredate")
	//写入Hash value
	ccw.HashValStore("demo2", "Hiredate", "2018-04-06")
	//读取Hash value
	ccw.HashValEject("demo2", "Email")

	//判断Hash field是否存在
	ccw.IsHashFieldExists("demo2", "Username")

	//删除整个Hash数据结构
	ccw.KeyDelete("demo2")

}
*/